/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.inpatient.service.impl;

import me.zhengjie.modules.his.domain.Pay;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.inpatient.repository.PayRepository;
import me.zhengjie.modules.his.inpatient.service.PayService;
import me.zhengjie.modules.his.inpatient.service.dto.PayDto;
import me.zhengjie.modules.his.inpatient.service.dto.PayQueryCriteria;
import me.zhengjie.modules.his.inpatient.service.mapstruct.PayMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class PayServiceImpl implements PayService {

    private final PayRepository payRepository;
    private final PayMapper payMapper;

    @Override
    public Map<String,Object> queryAll(PayQueryCriteria criteria, Pageable pageable){
        Page<Pay> page = payRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(payMapper::toDto));
    }

    @Override
    public List<PayDto> queryAll(PayQueryCriteria criteria){
        return payMapper.toDto(payRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PayDto findById(Integer payId) {
        Pay pay = payRepository.findById(payId).orElseGet(Pay::new);
        ValidationUtil.isNull(pay.getPayId(),"Pay","payId",payId);
        return payMapper.toDto(pay);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayDto create(Pay resources) {
        return payMapper.toDto(payRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Pay resources) {
        Pay pay = payRepository.findById(resources.getPayId()).orElseGet(Pay::new);
        ValidationUtil.isNull( pay.getPayId(),"Pay","id",resources.getPayId());
        pay.copy(resources);
        payRepository.save(pay);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer payId : ids) {
            payRepository.deleteById(payId);
        }
    }

    @Override
    public void download(List<PayDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PayDto pay : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("主院id", pay.getRegisterId());
            map.put("预交款", pay.getMoney());
            map.put("交款时间", pay.getPayDate());
            map.put("创建者", pay.getCreateBy());
            map.put("更新者", pay.getUpdateBy());
            map.put("创建日期", pay.getCreateTime());
            map.put("更新时间", pay.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}